﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class SymbolMap : EntityTypeConfiguration<Symbol> {

        public SymbolMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.eod_symbols");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(s => s.SymbolId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // IX_SYM_CD
            Property(s => s.ExchangeId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_SYM_CD", 1) { IsUnique = true }));
            Property(s => s.Code)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_SYM_CD", 2) { IsUnique = true }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // 1:0..n Exchanges
            HasRequired<Exchange>(s => s.Exchange)
                .WithMany()
                .HasForeignKey(s => s.ExchangeId);

            // Quotes
            HasMany<Quote>(a => a.Quotes)
                .WithRequired(a => a.Symbol)
                .HasForeignKey(a => a.SymbolId);

            // Corporate Actions
            HasMany<CorporateAction>(a => a.CorporateActions)
                .WithRequired(a => a.Symbol)
                .HasForeignKey(a => a.SymbolId);

            // Fundamentals
            HasMany<CorporateAction>(a => a.CorporateActions)
                .WithRequired(a => a.Symbol)
                .HasForeignKey(a => a.SymbolId);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(s => s.SymbolId)
                .HasColumnName("sym_id");

            Property(s => s.Code)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("sym_cd");

            Property(s => s.Name)
                .IsOptional()
                .IsUnicode(false)
                .HasMaxLength(250)
                .HasColumnName("sym_desc");

            Property(s => s.ExchangeId)
                .IsRequired()
                .HasColumnName("sym_exc_id");

            Ignore(s => s.ShortLable);

            Ignore(s => s.LongLable);

            Property(s => s.ValidFromDate)
                .IsRequired()
                .HasColumnType("date")
                .HasColumnName("sym_valid_frm_dt");

            Property(s => s.ValidTilDate)
                .IsOptional()
                .HasColumnType("date")
                .HasColumnName("sym_valid_til_dt");

            Property(s => s.LastLoadDate)
                .IsOptional()
                .HasColumnName("sym_last_load_ts");

            Property(s => s.IsYahoo)
                .IsOptional()
                .HasColumnName("sym_yahoo_fg");

            Property(s => s.YahooCode)
                .IsOptional()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("sym_yahoo_cd");

            // Bug fix 4: Avoid using google_fg for yahoo processing --> new flag
            Property(s => s.IsYahooProcessed)
                .IsOptional()
                .HasColumnName("sym_yahoo_procs_fg");

            Property(s => s.IsGoogle)
                .IsOptional()
                .HasColumnName("sym_google_fg");

            Property(s => s.GoogleCode)
                .IsOptional()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("sym_google_cd");
        }
    }
}
